package com.example.yskjproject;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;
import androidx.multidex.MultiDex;

import com.example.yskjproject.service.BottomDiscService;
import com.example.yskjproject.utils.AppInitializer;
import com.tencent.mmkv.MMKV;

public class App extends Application {


    private static App instance;
    public static Context context;

    private AppInitializer appInitializer;

    @Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(this);
        ScreenAdapter.setup(this);
        instance = this;
        appInitializer = new AppInitializer(this).initSP().initUPush().initOkGo();
        context = getApplicationContext();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifecycleObserver());

    }
    public  class AppLifecycleObserver implements LifecycleObserver {

        // 应用进入前台
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        public void onEnterForeground() {
            // 应用进入前台
            if (Settings.canDrawOverlays(getApplicationContext())) {
                // 权限已经被授予，可以开始服务
                startService(new Intent(getApplicationContext(), BottomDiscService.class));
            }
        }

        // 应用进入后台
        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        public void onEnterBackground() {
            // 应用进入后台
            stopService(new Intent(getApplicationContext(), BottomDiscService.class));
        }
    }

    public static  App getInstance(){
        return  instance;
    }
}
